package com.joycessh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.joycessh.PO.cdgl.VenuePO;
import com.joycessh.VO.cdgl.VenueVO;
import com.joycessh.dao.HibernateServiceProvider;
import com.joycessh.dao.ICdglDao;

public class CdglDaoImpl implements ICdglDao{
	
	HibernateServiceProvider hbp = new HibernateServiceProvider();

	@Override
	public boolean save(VenuePO venuepo) throws HibernateException {
		// TODO Auto-generated method stub
		boolean flag = true;
		Session session = hbp.getSession();	   
		Transaction t = null;
		try 
		{		
		    t = session.beginTransaction();
		    //如果在MonkeyPO中用了级联，则仅保存monkeypo即可
		    //	       session.save(teampo);          //如果没有级联，必须先保存team
		    session.save(venuepo);    //后保存monkey
		    t.commit();
		} 
		catch (HibernateException he) 
		{
		   if (t != null) 
		   {
		       t.rollback();
		       he.printStackTrace();   
		   }
		   flag = false;
		   throw he;
		}
		finally
			{
			    session.close();
			}
		return flag;	
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExistId(String venueId) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   //创建一个会话
		List<VenuePO> result = new ArrayList<VenuePO>();
	    Transaction tx = null;	 
	    boolean flag = false;
	    try
	    {		
	    	String hql = "from VenuePO as v where v.venueId = ? ";//使用占位符问号
	    	tx = session.beginTransaction();   //开始一个事务
	    	Query query = session.createQuery(hql);
	    	query.setString(0,venueId);
	    	result = query.list();   //结果集
	    	tx.commit(); //提交事务
	    }catch (HibernateException he) {
	      if (tx != null) {
	         tx.rollback();
	         he.printStackTrace(); 
	      }
	      flag = false;
	      throw he;
	    } finally {
	       session.close();
	    }
	    
	    if(result.iterator().hasNext()) {
	    	flag = true;
	    }else {
	    	flag = false;
	    }
	    return flag;
	}		

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExistName(String venueName) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   //创建一个会话
		List<VenuePO> result = new ArrayList<VenuePO>();
	    Transaction tx = null;	 
	    boolean flag = false;
	    try
	    {		
	    	String hql = "from VenuePO as v where v.venueName = ? ";//使用占位符问号
	    	tx = session.beginTransaction();   //开始一个事务
	    	Query query = session.createQuery(hql);
	    	query.setString(0,venueName);
	    	result = query.list();   //结果集
	    	tx.commit(); //提交事务
	    }catch (HibernateException he) {
	      if (tx != null) {
	         tx.rollback();
	         he.printStackTrace(); 
	      }
	      flag = false;
	      throw he;
	    } finally {
	       session.close();
	    }
	    
	    if(result.iterator().hasNext()) {
	    	flag = true;
	    }else {
	    	flag = false;
	    }
	    return flag;
	}

	@Override
	public boolean update(VenueVO venuevo) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   //创建一个会话
	    Transaction tx = null;	 
	    boolean flag = false;
	    int n;
	    try
	    {		
	    	String hql = "update VenuePO set venueName=?, sport=?, openTime=?, closeTime=?, courtNum=? where venueId=?";//使用占位符问号
	    	tx = session.beginTransaction();   //开始一个事务
	    	Query query = session.createQuery(hql);
	    	
	    	query.setString(0,venuevo.getVenueName());
	    	query.setString(1,venuevo.getSport());
	    	query.setTime(2,venuevo.getOpenTime());
	    	query.setTime(3,venuevo.getCloseTime());
	    	query.setString(4,venuevo.getCourtNum());
	    	query.setString(5,venuevo.getVenueId());
	    	
	    	n = query.executeUpdate();   //返回影响行数
	    	tx.commit(); //提交事务
	    }catch (HibernateException he) {
	      if (tx != null) {
	         tx.rollback();
	         he.printStackTrace(); 
	      }	      
	      throw he;
	    } finally {
	       session.close();
	    }
	    
	    if(n > 0) {
	    	flag = true;
	    }else {
	    	flag = false;
	    }
	    return flag;
	}

	@Override
	public boolean delete(String venueId) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   
	    Transaction tx = null;
	    boolean flag = false;
	    int n;
	    try {
	    	String hql= "delete VenuePO where venueId=?";
	    	tx = session.beginTransaction();   
	    	Query query = session.createQuery(hql);
	    	query.setString(0, venueId);
	    	
	    	n = query.executeUpdate(); 
	    	tx.commit();
	    	
	    }catch(HibernateException he) {
	    	if (tx != null) {
		         tx.rollback();
		         he.printStackTrace(); 
		      }	      
		      throw he;
	    }finally {
	    	session.close();
	    }
	    if(n > 0) {
	    	flag = true;
	    }else {
	    	flag = false;
	    }
	    return flag;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VenuePO> query(String queryText) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   
	    Transaction tx = null;
	    List<VenuePO> result = new ArrayList<VenuePO>();
	    
	    try
	    {
	      //按场馆名称，运动类型进行模糊查询
	      String hql = "from VenuePO as m where m.venueName like '%" +queryText+ "%' or m.sport like '%" +queryText+"%'";	  
 
	      tx = session.beginTransaction();   
	      Query query = session.createQuery(hql);
	      //query.setString(0,queryText); 
	      //query.setString(1, queryText);
	       
	      result = query.list();   
	      tx.commit();   
	    }catch (HibernateException he) 
	    {
	      if (tx != null) {
	         tx.rollback();
	      }
	      throw he;
	    } finally 
	    {
	       session.close();
	    }
	    
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VenuePO> showAll() throws HibernateException {
		// TODO Auto-generated method stub
		Session session = hbp.getSession();   
	    Transaction tx = null;
	    List<VenuePO> result = new ArrayList<VenuePO>();
	    
	    try
	    {
	      //按场馆名称，运动类型进行模糊查询
	      String hql = "from VenuePO";	  
 
	      tx = session.beginTransaction();   
	      Query query = session.createQuery(hql);
	      //query.setString(0,queryText); 
	      //query.setString(1, queryText);
	       
	      result = query.list();   
	      tx.commit();   
	    }catch (HibernateException he) 
	    {
	      if (tx != null) {
	         tx.rollback();
	      }
	      throw he;
	    } finally 
	    {
	       session.close();
	    }
	    
		return result;
	}

}
